(define merge-stack
  (case-lambda
   ((loc x)
    (cond
     ((and (list? x)(= (length x) 1))
      (car x))
     ((and (list? x)(string? (car x)))
      (apply string-append (reverse x)))
     ((and (list? x)(number? (car x)))
      (apply + x))
     ((and (list? x)(list? (car x)))
      (reverse x))
     (else (err-msg "[~a]merge-stack：[~a]格式不符合要求。" loc x)
	   #f)))
   ((loc x i)
    (cond
     ((and (list? x)(string? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(number? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(string? (list-ref (car x) i)))
      (apply string-append (map (lambda(l)(list-ref l i)) (reverse x))))
     ((and (list? x)(list? (car x))(number? (list-ref (car x) i)))
      (apply + (map (lambda(l)(list-ref l i)) x)))
     ((and (list? x)(list? (car x))(list? (list-ref (car x) i)))
      (reverse x))
     (else (err-msg "[~a]merge-stack：[~a][~a]格式不符合要求。" loc x i)
	   #f)))))
(define merge-str
  (case-lambda
   ((loc x)
    (cond
     ((null? x) "")
     ((string? x) x)
     ((and (list? x)(= (length x) 1)(string? (car x)))(car x))
     ((and (list? x)(string? (car x)))
      (apply string-append (reverse x)))
     ((and (list? x)(list (car x))(string? (car (car x))))
      (apply string-append (map (lambda(l)(car l)) (reverse x))))
     (else (err-msg "[~a]merge-str：[~a]格式不符合要求。" loc x)
	   #f)))
   ((loc x i)
    (cond
     ((null? x) "")
     ((and (list? x)(string? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(string? (list-ref (car x) i)))
      (apply string-append (map (lambda(l)(list-ref l i)) (reverse x))))
     (else (err-msg "[~a]merge-str：[~a][~a]格式不符合要求。" loc x i)
	   #f)))))
(define (merge-str-blank l)
  (if (null? l) ""
      (let ((res-s (car l)))
	(begin
	  (tr-list (i e l 1)
		   (set! res-s (string-append res-s " " e)))
	  res-s))))
(define merge-num
  (case-lambda
   ((loc x)
    (cond
     ((number? x) x)
     ((and (list? x)(= (length x) 1)(number? (car x)))(car x))
     ((and (list? x)(number? (car x)))
      (apply + x))
     ((and (list? x)(list (car x))(number? (car (car x))))
      (apply + (map (lambda(l)(car l)) x)))
     (else (err-msg "[~a]merge-num：[~a]格式不符合要求。" loc x)
	   #f)))
   ((loc x i)
    (cond
     ((and (list? x)(> (length x) i)(number? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(> (length (car x)) i)(number? (list-ref (car x) i)))
      (apply + (map (lambda(l)(list-ref l i)) x)))
     (else (err-msg "[~a]merge-num：[~a][~a]格式不符合要求。" loc x i)
	   #f)))))
(define merge-stack1
  (case-lambda
   ((x)
    (cond
     ((and (list? x)(= (length x) 1))
      (car x))
     ((and (list? x)(string? (car x)))
      (apply string-append (reverse x)))
     ((and (list? x)(number? (car x)))
      (apply + x))
     ((and (list? x)(list? (car x)))
      (reverse x))
     (else (err-msg "merge-stack：[~a]格式不符合要求。" x)
	   #f)))
   ((x i)
    (cond
     ((and (list? x)(string? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(number? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(string? (list-ref (car x) i)))
      (apply string-append (map (lambda(l)(list-ref l i)) (reverse x))))
     ((and (list? x)(list? (car x))(number? (list-ref (car x) i)))
      (apply + (map (lambda(l)(list-ref l i)) x)))
     ((and (list? x)(list? (car x))(list? (list-ref (car x) i)))
      (reverse x))
     (else (err-msg "merge-stack：[~a][~a]格式不符合要求。" x i)
	   #f)))))
(define merge-str1
  (case-lambda
   ((x)
    (cond
     ((string? x) x)
     ((and (list? x)(= (length x) 1)(string? (car x)))(car x))
     ((and (list? x)(string? (car x)))
      (apply string-append (reverse x)))
     ((and (list? x)(list (car x))(string? (car (car x))))
      (apply string-append (map (lambda(l)(car l)) (reverse x))))
     (else (err-msg "merge-str：[~a]格式不符合要求。" x)
	   #f)))
   ((loc x i)
    (cond
     ((and (list? x)(string? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(string? (list-ref (car x) i)))
      (apply string-append (map (lambda(l)(list-ref l i)) (reverse x))))
     (else (err-msg "merge-str：[~a][~a]格式不符合要求。" x i)
	   #f)))))

(define merge-num1
  (case-lambda
   ((x)
    (cond
     ((number? x) x)
     ((and (list? x)(= (length x) 1)(number? (car x)))(car x))
     ((and (list? x)(number? (car x)))
      (apply + x))
     ((and (list? x)(list (car x))(number? (car (car x))))
      (apply + (map (lambda(l)(car l)) x)))
     (else (err-msg "merge-num：[~a]格式不符合要求。" x)
	   #f)))
   ((x i)
    (cond
     ((and (list? x)(number? (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(number? (list-ref (car x) i)))
      (apply + (map (lambda(l)(list-ref l i)) x)))
     (else (err-msg "merge-num：[~a][~a]格式不符合要求。" x i)
	   #f)))))
(define merge-type
  (case-lambda
   ((fp f x)
    (cond
     ((fp x) x)
     ((and (list? x)(= (length x) 1)(fp (car x)))(car x))
     ((and (list? x)(fp (car x)))
      (apply f (reverse x)))
     ((and (list? x)(list (car x))(fp (car (car x))))
      (apply f (map (lambda(l)(car l)) (reverse x))))
     (else #f)))
   ((fp f x i)
    (cond
     ((and (list? x)(fp (list-ref x i)))
      (list-ref x i))
     ((and (list? x)(list? (car x))(fp (list-ref (car x) i)))
      (apply f (map (lambda(l)(list-ref l i)) (reverse x))))
     (else #f)))))
(define merge-number
  (case-lambda
   ((x) (merge-type number? + x))
   ((x i) (merge-type number? + x i))))
(define merge-string
  (case-lambda
   ((x) (merge-type string? string-append x))
   ((x i) (merge-type string? string-append x i))))

(define (merge-list loc l)
   (cond
       ((and (pair? l)(>= (length l) 5)(integer? (cadr l)))
	l)
       ((and (pair? l)(= (length l) 1)(pair? (car l)))
	(car l))
       ((and (pair? l)(> (length l) 1)(pair? (car l))(pair? (cadr l)))
	(reverse l))
       (else (err-msg "[~a]merge-list：[~a]不是合规的cyl格式。" loc l)
	     #f)))
(define (merge-cyl loc l st)
  ;;(y-or-n "TEST-merge-cyl:loc=~a,l=~a,st=~a." loc l st)
  (if st
      (cond
       ((and (pair? l)(>= (length l) 5)(integer? (cadr l)))
	l)
       ((and (pair? l)(= (length l) 1)(pair? (car l))(>= (length (car l)) 5)(integer? (cadr (car l))))
	(car l))
       ((and (pair? l)(> (length l) 1)(pair? (car l))(pair? (cadr l)))
	(list (merge-list loc l) (merge-num loc l 1) 0 st (cyl-serial)))
       (else (err-msg "[~a]merge-cyl：[~a]不是合规的cyl格式。" loc l)
	     #f))
      (begin
	(err-msg "[~a]merge-cyl：传入状态为#f。" loc)
	#f)))
(define (merge-cyl-fn loc l st fn par)
  ;;(y-or-n "TEST-merge-cyl:loc=~a,l=~a,st=~a." loc l st)
  (if st
      (if (and (pair? l)(= (length l) 1))
	  (begin
	    ;;(y-or-n "merge-fn-cyl:l=~a,len=~a,(car l)=~a,(nth 1 (car l))=~a.\n" l (length l) (car l) (nth 1 (car l)))
	    (list (reverse l) (nth 1 (car l)) 0 st (cyl-serial) fn par)
	    )
	  (list (reverse l) (merge-num loc l 1) 0 st (cyl-serial) fn par))
      (begin
	(err-msg "[~a]merge-cyl：传入状态为#f。" loc)
	#f)))    
(define (make-cyl loc l st)
  (cond
   ((and (eq? st '词语)(string? l))
    (list l (string-length l) '() st (cyl-serial)))
   ((and (eq? (car st) '调用)(string? l))
    (let-values (((cur-coding cur-type cur-record)(cyl-coding-get loc l)))
      (if (not cur-coding) (set! cur-type (cadr st)))
      (list l (string-length l) cur-coding cur-type (cyl-serial))))
   ((and (st-is st '常量) (string? l))
    (list l (string-length l) 0 st (cyl-serial)))
   ))
   
(define (merge-lisp loc lang-l st)
  (merge-stack loc lang-l)
  )
